package com.example.stock.util;

import com.alibaba.excel.EasyExcel;
import com.example.stock.excel.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Log4j2
public class DataUtil {

    public final static String STOCK_SPLIT = ";";

    public static <T> List<T> getListData(Class<T> tClass,String fileName) {
        BaseListReadListener<T> listener = new BaseListReadListener<>();
        try {
            File file = ResourceUtils.getFile(fileName);
            EasyExcel.read(file, tClass, listener).doReadAll();
        } catch (FileNotFoundException e) {
            log.error(fileName + " not found");
        }
        return listener.getList();
    }

    public static <T> List<T> getListData(Class<T> tClass,String fileName, String sheetName) {
        BaseListReadListener<T> listener = new BaseListReadListener<>();
        try {
            File file = ResourceUtils.getFile(fileName);
            EasyExcel.read(file, tClass, listener).sheet(sheetName).doRead();
        } catch (FileNotFoundException e) {
            log.error(fileName + " " + sheetName + " not found");
        }
        return listener.getList();
    }

    static List<DailyMonitor> dailyMonitorData;

    public static List<DailyMonitor> getDailyMonitorData() {
        if (dailyMonitorData == null) {
            dailyMonitorData = getListData(DailyMonitor.class, ExcelReaderUtil.FILE_DAILY_MONITOR);
        }
        return dailyMonitorData;
    }

    static List<DailyDeSum> dailyDeSumData;

    public static List<DailyDeSum> getDailyDeSumData() {
        if (dailyDeSumData == null) {
            dailyDeSumData = getListData(DailyDeSum.class, ExcelReaderUtil.FILE_DAILY_DE_SUM);
        }
        return dailyDeSumData;
    }

    static List<DailyLog> dailyLogs;

    public static List<DailyLog> getDailyLogs() {
        if (Objects.isNull(dailyLogs)) {
            dailyLogs = getListData(DailyLog.class, ExcelReaderUtil.FILE_OPERATION,"日记");
        }
        return dailyLogs;
    }


    // 公众号文章数据
    static List<Article> articleData;

    public static List<Article> getArticleData() {
        if (Objects.isNull(articleData)) {
            articleData = getListData(Article.class, ExcelReaderUtil.FILE_STOCK_ARTICLE);
        }
        return articleData;
    }

    static List<StockInfo> stockInfos;

    public static List<StockInfo> getStockInfos() {
        if (Objects.isNull(stockInfos)) {
            stockInfos = getListData(StockInfo.class, ExcelReaderUtil.FILE_STOCK_LIST, "stocks");
        }
        return stockInfos;
    }

    // 金融数据
    static List<Finance> financeList;

    public static List<Finance> getFinanceData() {
        if (Objects.isNull(financeList)) {
            financeList = getListData(Finance.class, ExcelReaderUtil.FILE_STOCK_FINANCE, "finance_data");
        }
        return financeList;
    }

    static List<YearPctStat> yearPctStatList;

    public static List<YearPctStat> getYearPctStatData() {
        if (Objects.isNull(yearPctStatList)) {
            yearPctStatList = getListData(YearPctStat.class, ExcelReaderUtil.FILE_YEAR_PCT_SAT, "年统计");
        }
        return yearPctStatList;
    }

    static List<StockIntroduce> stockIntroduces;

    public static List<StockIntroduce> getStockIntroduces() {
        if (Objects.isNull(stockIntroduces)){
            stockIntroduces = getListData(StockIntroduce.class, ExcelReaderUtil.FILE_OPERATION,"简介");
        }
        return stockIntroduces;
    }

    static List<Wugu> wuguList;

    public static List<Wugu> getWuguList() {
        if (Objects.isNull(wuguList)) {
            wuguList = getListData(Wugu.class, ExcelReaderUtil.FILE_OPERATION, "吾股");
        }

        return wuguList;
    }

    static List<MinYearPct> minYearPctList;

    public static List<MinYearPct> getMinYearPctList() {
        if (Objects.isNull(minYearPctList)) {
            minYearPctList = getListData(MinYearPct.class, ExcelReaderUtil.FILE_YEAR_PCT_SAT, "min_year_pct");
        }
        return minYearPctList;
    }

    static List<DividendPayoutRatio> dividendPayoutRatioList;

    public static List<DividendPayoutRatio> getDividendPayoutRatioList() {
        if (Objects.isNull(dividendPayoutRatioList)) {
            dividendPayoutRatioList = getListData(DividendPayoutRatio.class, ExcelReaderUtil.FILE_OPERATION,"派现融资比");
        }

        return dividendPayoutRatioList;
    }
}